import java.awt.Button;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.MalformedURLException;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;

/**
 * @author salim
 *
 */

public class TestStep3 extends Frame {

	public TextArea		textMessage;
	public TextField	dataMessage;
	
	public TextArea		textComment;
	public TextField	dataComment;
	
	Message_itf		message;
	Comment_itf		comment;
	
	static String		myName;
	static boolean		isCommentCreator;

	public static void main(String argv[]) throws RemoteException, MalformedURLException, NotBoundException {
		
		if (argv.length < 1) {
			System.out.println("java TestStep3 <name>");
			return;
		}
		myName = argv[0];
		
		if (argv.length == 1) {
			isCommentCreator = true; 
		}
	
		// initialize the system
		Client.init();
		
		// look up the comment object in the name server
		// if not found, create it, and register it in the name server
		// ACTION TO PERFORM IF IAM A COMMENT CREATOR
		
		Comment_itf comment = null;
		
		if (isCommentCreator){
			comment = (Comment_itf)Client.lookup("comment");
			if (comment == null) {
				comment = (Comment_itf)Client.create(new Comment());
				Client.register("comment", comment);
			}
		}
		
		// look up the message object in the name server
		// if not found, create it (reference it to the sharedObject comment, and register it in the name server
		Message_itf message = (Message_itf)Client.lookup("message");
		if (message == null) {
			if (isCommentCreator) {
				message = (Message_itf)Client.create(new Message(comment));
			}else {
				message = (Message_itf)Client.create(new Message());
			}
			Client.register("message", message);
		}
		
		// create the graphical part
		new TestStep3(message);
	}

	public TestStep3(Message_itf message) {
	
		setLayout(new FlowLayout());
	
		textMessage =new TextArea(10,60);
		textComment =new TextArea(10,60);
		
		textMessage.setEditable(false);
		textComment.setEditable(false);
		
		textMessage.setForeground(Color.red);
		add(textMessage);
		textComment.setForeground(Color.red);
		add(textComment);
	
		dataMessage=new TextField(60);
		add(dataMessage);
		
		Button writeMessage_button = new Button("write message");
		writeMessage_button.addActionListener(new writeListener2(this));
		add(writeMessage_button);
		
		Button readMessage_button = new Button("read message");
		readMessage_button.addActionListener(new readListener2(this));
		add(readMessage_button);
		
		dataComment=new TextField(60);
		add(dataComment);
		
		Button writeComment_button = new Button("write comment");
		writeComment_button.addActionListener(new writeListener2b(this));
		add(writeComment_button);
		
		Button readComment_button = new Button("read comment");
		readComment_button.addActionListener(new readListener2b(this));
		add(readComment_button);
		
		setSize(1200,270);
		textMessage.setBackground(Color.black);
		textComment.setBackground(Color.green);
		show();
		
		this.message = message;
		this.comment = message.getComment();
	}
}

class readListener2 implements ActionListener {
	TestStep3 testStep3;
	public readListener2 (TestStep3 i) {
		testStep3 = i;
	}
	public void actionPerformed (ActionEvent e) {

		// lock the object in read mode
		testStep3.message.lock_read();

		// invoke the method
		String s = testStep3.message.read();

		// unlock the object
		testStep3.message.unlock();

		// display the read value
		testStep3.textMessage.append(s+"\n");
	}
}

class writeListener2 implements ActionListener {
	TestStep3 testStep3;
	public writeListener2 (TestStep3 i) {
		testStep3 = i;
	}
	public void actionPerformed (ActionEvent e) {

		// get the value to be written from the buffer
		String s = testStep3.dataMessage.getText();

		// lock the object in write mode
		testStep3.message.lock_write();

		// invoke the method
		testStep3.message.write(TestStep3.myName+" wrote "+s);
		testStep3.dataMessage.setText("");

		// unlock the object
		testStep3.message.unlock();
	}
}

class readListener2b implements ActionListener {
	TestStep3 testStep3;
	public readListener2b (TestStep3 i) {
		testStep3 = i;
	}
	public void actionPerformed (ActionEvent e) {

		// lock the object in read mode
		testStep3.comment.lock_read();

		// invoke the method
		String s = testStep3.comment.read();

		// unlock the object
		testStep3.comment.unlock();

		// display the read value
		testStep3.textComment.append(s+"\n");
	}
}

class writeListener2b implements ActionListener {
	TestStep3 testStep3;
	public writeListener2b (TestStep3 i) {
		testStep3 = i;
	}
	public void actionPerformed (ActionEvent e) {

		// get the value to be written from the buffer
		String s = testStep3.dataComment.getText();

		// lock the object in write mode
		testStep3.comment.lock_write();

		// invoke the method
		testStep3.comment.write(TestStep3.myName+" wrote "+s);
		testStep3.dataComment.setText("");

		// unlock the object
		testStep3.comment.unlock();
	}
}